package top.chmin.project.cloud.common.dict.type.auto;

// field import auto
// #bind.field.dictName#

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.entity.Page;

import java.util.List;
import java.util.stream.Stream;


/**
 * chmin
 * 2023-12-27 16:04:10
 * cloud-builder
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DictTypeService {

    @Resource
    DictTypePlugin plugin;

    @Resource
    DictTypeDao dictTypeDao;

// field labelDao auto

    public int save(DictTypeSave.Param param) {
        plugin.checkSave(param);
        DictType dictType = BeanUtil.toBean(param, DictType.class);
        plugin.beforeSave(dictType);
        int result = dictTypeDao.save(dictType);
        plugin.afterSave(param, dictType, result);
        return result;
    }

    public int updateById(DictTypeUpdateById.Param param) {
        plugin.checkUpdateById(param);
        DictType dictType = BeanUtil.toBean(param, DictType.class);
        plugin.beforeUpdateById(dictType);
        int result = dictTypeDao.updateById(dictType);
        plugin.afterUpdateById(param, dictType, result);
        return result;
    }

    public int deleteById(DictTypeDeleteById.Param param) {
        plugin.checkDeleteById(param);
        int result = dictTypeDao.deleteById(param.getId());
        plugin.afterDeleteById(param, result);
        return result;
    }

    public DictTypeSelectById.Result selectById(DictTypeSelectById.Param param) {
        plugin.checkSelectById(param);
        DictTypeFPC fpc = new DictTypeFPC().id().eq(param.getId());
        plugin.beforeSelectById(param, fpc);
        DictTypeSelectById.Result result = dictTypeDao.getFirst(fpc.toCondition(), DictTypeSelectById.Result.class);
        plugin.afterSelectById(param, result);
        return result;
    }

    public Page<DictTypeSelectPage.Result> selectPage(DictTypeSelectPage.Param param) {
        plugin.checkSelectPage(param);
        DictTypeFPC fpc = new DictTypeFPC();
        fpc = param.isDesc() ? fpc.id().desc() : fpc.id().asc();
        plugin.beforeSelectPage(param, fpc);
        Page<DictTypeSelectPage.Result> result = dictTypeDao.getPageByCondition(fpc.toCondition(), DictTypeSelectPage.Result.class);
// field label auto
        plugin.afterSelectPage(param, result);
        return result;
    }

// entity select auto

// entity search auto
// entity search auto
public List<DictTypeDictCodeSearch.Result> searchDictCode(DictTypeDictCodeSearch.Param param) {
    plugin.checkSearchDictCode(param);
    DictTypeFPC fpc = new DictTypeFPC();
    fpc.limit(10);
    plugin.beforeSearchDictCode(param, fpc);
    Stream<DictTypeDictCodeSearch.Result> result = dictTypeDao.getStreamByCondition(fpc.toCondition(), DictTypeDictCodeSearch.Result.class)
            .peek(r -> {
                r.setValue(r.getDictCode());
                r.setLabel(r.getDictName());
            });
    List<DictTypeDictCodeSearch.Result> list = result.toList();
    plugin.afterSearchDictCode(param, list);
    return list;
}



}
